import { ModalForm, ProFormDateTimePicker, ProFormText } from '@ant-design/pro-components';
import { message } from 'antd';
import { outOrder } from '../service';
import { OutOrderParams } from '../types.d';

const OutOrder = (props: { visible: boolean; orderNo: string; close: () => void }) => {
  const { visible = false, orderNo, close } = props;
  return (
    <ModalForm<OutOrderParams>
      title="驶出订单"
      layout="horizontal"
      labelCol={{ span: 6 }}
      wrapperCol={{ span: 16 }}
      width={800}
      visible={visible}
      modalProps={{
        destroyOnClose: true,
        maskClosable: false,
        onCancel: () => {
          close();
        },
      }}
      onFinish={async (values) => {
        const outData: OutOrderParams = {
          orderNo: orderNo,
        };
        if (values.endTime) {
          outData.endTime = values.endTime;
        }
        const res = await outOrder(outData);
        if (res?.success) {
          message.success({ content: '驶出成功', key: 'out-order' });
          close();
          return true;
        }
        message.error({ content: res?.message || '驶出失败', key: 'out-order' });
        return false;
      }}
    >
      <ProFormText
        width="lg"
        label="订单编号"
        required
        disabled
        initialValue={orderNo}
        name="orderNo"
      />
      <ProFormDateTimePicker
        width="lg"
        label="驶出时间"
        tooltip="不选默认为操作时间"
        name="endTime"
      />
    </ModalForm>
  );
};

export default OutOrder;
